<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>求数组交集2</title>
</head>
<body>
<script>
    let arr1 = [1, 2, 3]
    let arr2 = [2, 4, 2]
    let intersection = function (nums1, nums2) {
        let res = [];
        let shortArr = null;
        let longArr = null;
        //把对应的长度放到对应的数组内
        if (nums1.length > nums2.length) {
            shortArr = nums2;
            longArr = nums1;
        } else {
            shortArr = nums1;
            longArr = nums2;
        }
        for (let i = 0; i < shortArr.length;i++){
            const cur = shortArr[i];
            if (longArr.includes(cur)){
                res.push(cur);
            }
        }
        return Array.from(new Set(res));
    }
    console.log(intersection(arr1,arr2));
</script>
</body>
</html>